﻿using Dapper;
using System;
using BCEntity.Trading.Purchase.GoodsPurchaseContractAttachment;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Trading.Purchase.GoodsPurchaseContractAttachment
{
    /// <summary>
    /// 商品采购合同附件
    /// </summary>
    public class GoodsPurchaseContractAttachmentData : IGoodsPurchaseContractAttachmentData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM goodspurchasecontractattachment";
        private const string QUERY = @"SELECT GoodsPurchaseContractAttachmentId,GoodsPurchaseId,GoodsPurchaseNo,PurchaseContractNo,AttachmentAddress,CreateTime,CreateAdminId,CreateAdminName FROM goodspurchasecontractattachment";
        public GoodsPurchaseContractAttachmentData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查商品采购合同附件是否存在
        public async Task<bool> Exists(long goodsPurchaseContractAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE GoodsPurchaseContractAttachmentId=?GoodsPurchaseContractAttachmentId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchaseContractAttachmentId", goodsPurchaseContractAttachmentId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加商品采购合同附件
        public async Task<GoodsPurchaseContractAttachmentEntity> Add(GoodsPurchaseContractAttachmentEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("goodspurchasecontractattachment", new string[] { "GoodsPurchaseId", "GoodsPurchaseNo", "PurchaseContractNo", "AttachmentAddress", "CreateAdminId", "CreateAdminName" });
            sql = string.Format("{0} {1} WHERE GoodsPurchaseContractAttachmentId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchaseId", entity.GoodsPurchaseId, DbType.Int64, size: 20);
            parameters.Add("?GoodsPurchaseNo", entity.GoodsPurchaseNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?PurchaseContractNo", entity.PurchaseContractNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?AttachmentAddress", entity.AttachmentAddress?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<GoodsPurchaseContractAttachmentEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改商品采购合同附件
        public async Task<GoodsPurchaseContractAttachmentEntity> Update(GoodsPurchaseContractAttachmentEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("goodspurchasecontractattachment", new string[] { "GoodsPurchaseId", "GoodsPurchaseNo", "PurchaseContractNo", "AttachmentAddress", "CreateAdminId", "CreateAdminName" }, new string[] { "GoodsPurchaseContractAttachmentId" });
            sql = string.Format("{0} {1} WHERE GoodsPurchaseContractAttachmentId=?GoodsPurchaseContractAttachmentId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchaseContractAttachmentId", entity.GoodsPurchaseContractAttachmentId, DbType.Int64, size: 20);
            parameters.Add("?GoodsPurchaseId", entity.GoodsPurchaseId, DbType.Int64, size: 20);
            parameters.Add("?GoodsPurchaseNo", entity.GoodsPurchaseNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?PurchaseContractNo", entity.PurchaseContractNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?AttachmentAddress", entity.AttachmentAddress?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<GoodsPurchaseContractAttachmentEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除商品采购合同附件
        public async Task<bool> Delete(long goodsPurchaseContractAttachmentId)
        {
            string sql = @"DELETE FROM goodspurchasecontractattachment WHERE GoodsPurchaseContractAttachmentId=?GoodsPurchaseContractAttachmentId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchaseContractAttachmentId", goodsPurchaseContractAttachmentId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取商品采购合同附件
        public async Task<GoodsPurchaseContractAttachmentEntity> GetEntity(long goodsPurchaseContractAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE GoodsPurchaseContractAttachmentId=?GoodsPurchaseContractAttachmentId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchaseContractAttachmentId", goodsPurchaseContractAttachmentId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<GoodsPurchaseContractAttachmentEntity>(sql, parameters);
        }
        #endregion

        #region 获取商品采购合同附件列表
        public async Task<IEnumerable<GoodsPurchaseContractAttachmentEntity>> GetList(long goodsPurchaseId)
        {
            string sql = string.Format(@"{0} WHERE GoodsPurchaseId=?GoodsPurchaseId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchaseId", goodsPurchaseId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<GoodsPurchaseContractAttachmentEntity>(sql,parameters);
        }
        #endregion

        #region 获取商品采购合同附件分页
        public async Task<(IEnumerable<GoodsPurchaseContractAttachmentEntity>, int)> Query(long? goodsPurchaseId,string purchaseContractNo, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(purchaseContractNo))
            {
                wheres.Add("GoodsPurchaseNo LIKE CONCAT('%',CONCAT(?GoodsPurchaseNo,'%'))");
                parameters.Add("?GoodsPurchaseNo", purchaseContractNo?.Trim(), DbType.AnsiString);
            }
            if (goodsPurchaseId.HasValue)
            {
                wheres.Add("GoodsPurchaseId=?GoodsPurchaseId");
                parameters.Add("?GoodsPurchaseId", goodsPurchaseId, DbType.Int64);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM goodspurchasecontractattachment {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<GoodsPurchaseContractAttachmentEntity, int>(sql, parameters);
        }
        #endregion
    }
}



